package com.comac.print.component.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comac.print.component.app.domain.PrintMRecord;
import com.comac.print.component.app.domain.dto.PrintRecordSearchDTO;
import com.comac.print.component.app.mapper.PrintMRecordMapper;
import com.comac.print.component.app.page.PageResponse;
import com.comac.print.component.app.service.PrintMRecordService;
import com.comac.print.component.app.utils.ZipUtil;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 服务调用记录表 服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2025-04-16 03:51:14
 */
@Service
public class PrintMRecordServiceImpl extends ServiceImpl<PrintMRecordMapper, PrintMRecord> implements PrintMRecordService {

    /**
     * 分页查询服务调用记录表
     *
     * @param searchDTO
     * @return
     */
    @Override
    public PageResponse<PrintMRecord> searchPage(PrintRecordSearchDTO searchDTO) {
        LambdaQueryWrapper<PrintMRecord> queryWrapper = createLambdaQueryWrapper(searchDTO);
        Page<PrintMRecord> page = super.page(new Page<>(searchDTO.getPageNum(), searchDTO.getPageSize()),
                queryWrapper);
        return PageResponse.successPage(page);
    }

    private LambdaQueryWrapper<PrintMRecord> createLambdaQueryWrapper(PrintRecordSearchDTO searchDTO) {
        LambdaQueryWrapper<PrintMRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(PrintMRecord::getId, PrintMRecord::getSystemId, PrintMRecord::getBussinessId,
                        PrintMRecord::getPrintFileUrl, PrintMRecord::getIsSuccess, PrintMRecord::getErrMessage,
                        PrintMRecord::getOperatorTime)
                .eq(StrUtil.isNotBlank(searchDTO.getBussinessId()),
                        PrintMRecord::getBussinessId, searchDTO.getBussinessId())
                .like(StrUtil.isNotBlank(searchDTO.getSystemId()),
                        PrintMRecord::getSystemId, searchDTO.getSystemId())
                .eq(StrUtil.isNotBlank(searchDTO.getMpId()),
                        PrintMRecord::getMpId, searchDTO.getMpId())
                .like(StrUtil.isNotBlank(searchDTO.getMpUrl()),
                        PrintMRecord::getMpUrl, searchDTO.getMpUrl())
                .eq(searchDTO.getIsSuccess() != null,
                        PrintMRecord::getIsSuccess, searchDTO.getIsSuccess())
                .between(null != searchDTO.getOperatorBeginTime() && null != searchDTO.getOperatorEndTime(),
                        PrintMRecord::getOperatorTime, searchDTO.getOperatorBeginTime(), searchDTO.getOperatorEndTime())
                .eq(PrintMRecord::getDeleteFlag, false);
        queryWrapper.orderByDesc(PrintMRecord::getOperatorTime);
        return queryWrapper;
    }

    public PrintMRecord getById(Long id){
        PrintMRecord record = super.getById(id);
        if (Objects.isNull(record)){
            return record;
        }
        if (StrUtil.isNotBlank(record.getPrintData())){
            String printData = ZipUtil.unzipBae64(record.getPrintData());
            record.setPrintData(printData);
        }
        return record;
    }

}
